package com.ys.peony.utils;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.ys.peony.app.BaseApplication;
import com.ys.peony.model.YWDJPrepareAreaItemModel;
import com.ys.peony.model.YWDJPrepareDanBaoItemModel;
import com.ys.peony.model.YWDJPrepareItemModel;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通用工具类
 *
 * @author Hunter
 */
public class CommonUtils {
    private static final String GSON_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 对指定字符串进行md5加密
     *
     * @param s
     * @return 加密后的数据
     */
    public static String EncryptMD5(String s) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 判断email格式是否正确
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 判断identityCard格式是否正确
     *
     * @param idCard
     * @return
     */
    public static boolean isIdentityCard(String idCard) {
        String str1 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
        String str2 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
        Pattern p1 = Pattern.compile(str1);
        Pattern p2 = Pattern.compile(str2);
        if (p1.matcher(idCard).matches() || p2.matcher(idCard).matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据系统语言判断是否为中国
     *
     * @return
     */
    public static boolean isZh() {
        Locale locale = BaseApplication.getInstance().getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        if (language.startsWith("zh")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public static String getVersion() {
        try {
            PackageManager manager = BaseApplication.getInstance().getPackageManager();
            PackageInfo info = manager.getPackageInfo(BaseApplication.getInstance().getPackageName(), 0);
            return info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * describe:判断字符串是否为空
     * author:zhangcong
     * date:2017/5/8 23:47
     */
    public static boolean isNotEmpty(String str) {
        str = str.replaceAll("\\s*", "");
        if (null != str && !"".equals(str)) {
            return true;
        }
        return false;
    }

    public static String getTextbyVal(String val, List<YWDJPrepareItemModel> list) {
        String textStr = "";
        if (list == null) {
            return "";
        }
        for (int i = 0; i < list.size(); i++) {
            if (val.equals(list.get(i).getVal())) {
                textStr = list.get(i).getText();
                break;
            }
        }
        return textStr;
    }

    public static String getAreaByCodeStr(int val, List<YWDJPrepareAreaItemModel> areaList) {
        String areaStr = null;
        for (int i = 0; i < areaList.size(); i++) {
            if (val == areaList.get(i).getId()) {
                areaStr = areaList.get(i).getBrand() + "(" + areaList.get(i).getName() + ")";
                break;
            }
            areaStr = getAreaByCodeStr(val, areaList.get(i).getSysAreaList());
            if (!StringUtils.isEmpty(areaStr)) {
                break;
            }
        }
        return areaStr;
    }

    public static List<String> getOptionTextList(List<YWDJPrepareItemModel> options) {
        List<String> optionTextList = new ArrayList<String>();
        for (int i = 0; i < options.size(); i++) {
            optionTextList.add(options.get(i).getText());
        }
        return optionTextList;
    }

    public static List<String> getOptionTextList2(List<YWDJPrepareDanBaoItemModel> options) {
        List<String> optionTextList = new ArrayList<String>();
        for (int i = 0; i < options.size(); i++) {
            optionTextList.add(options.get(i).getName());
        }
        return optionTextList;
    }

    public static String getRandNum(int min, int max) {
        int randNum = min + (int)(Math.random() * ((max - min) + 1));
        return String.valueOf(randNum);
    }
}
